package com.checx.loader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MyClassLoader extends ClassLoader{
    private String path = "";

    public MyClassLoader(String path) {
        this.path = path;
    }

    @Override
    public synchronized Class<?> loadClass(String name) {
        Class<?> cls = findLoadedClass(name);
        if(cls != null) return cls;
        try{
            if(!name.endsWith("Spoofed") && !name.endsWith("Sub") && !name.endsWith("delegating.C"))
                return findSystemClass(name);
        } catch (ClassNotFoundException e) {
            // todo
        }
        try {
            byte[] data = getClassData(name);
            return defineClass(name, data, 0, data.length);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private byte[] getClassData(String file) throws IOException {
        String[] arr = file.split("\\.");
        return Files.readAllBytes(Paths.get(path, arr[arr.length - 1] + ".class"));
    }
}

// target/classes/com/checx/reload/Service.class